package com.example.teamtribe.hello;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.example.teamtribe.R;
import com.example.teamtribe.base.BaseActivity;
import com.example.teamtribe.main.HomeActivity;
import com.example.teamtribe.utils.Code;

public class LoginActivity extends BaseActivity implements View.OnClickListener, HelloContract.View {

    private RelativeLayout rlCode;
    private EditText etUser;
    private EditText etPsw;
    private EditText etCode;
    private ImageView ivCode;
    private String code;
    private HelloContract.Presenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        rlCode = findViewById(R.id.rl_code);
        etUser = findViewById(R.id.et_user);
        etPsw = findViewById(R.id.et_psw);
        etCode = findViewById(R.id.et_code);
        ivCode = findViewById(R.id.iv_code);
        presenter = new HelloPresenter(this);

        ivCode.setOnClickListener(this);
        findViewById(R.id.bt_login).setOnClickListener(this);
        findViewById(R.id.bt_register).setOnClickListener(this);
        findViewById(R.id.tv_forget).setOnClickListener(this);
    }

    @Override
    public void updateCode() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                rlCode.setVisibility(View.GONE);
                ivCode.setImageBitmap(Code.getInstance().createBitmap());
                code = Code.getInstance().getCode().toLowerCase();
            }
        });
    }

    @Override
    public void next() {
        startActivity(new Intent(LoginActivity.this, HomeActivity.class));
        finish();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_login:
                String username = etUser.getText().toString();
                String psw = etPsw.getText().toString();
                if (TextUtils.isEmpty(username) || TextUtils.isEmpty(psw)) {
                    showToast("用户名或密码不能为空");
                    return;
                }
                if (rlCode.getVisibility() == View.VISIBLE) {
                    if (!etCode.getText().toString().equals(code)) {
                        showToast("验证码错误");
                        updateCode();
                        return;
                    }
                }
                presenter.login(username, psw);
                break;

            case R.id.bt_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                finish();
                break;

            case R.id.iv_code:
                updateCode();
                break;

            case R.id.tv_forget:
                //忘记密码
                break;

            default:
                break;
        }
    }
}
